<?php
/**
 * Created by PhpStorm.
 * User: sqq
 * Date: 2018/08/1
 * Time: 14:48
 * C端优惠券  api3.0
 */

class ApiCouponController extends BaseController{

    /**
     * @Author:      sqq
     * @DateTime:    2018-07-28 15:02:16
     * @Description: 优惠券推送
     * @link:
     */
    public function pushCoupon(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'coupon_id' => 'required',
                'coupon_base_info_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $coupon_id = Input::get('coupon_id');
            $coupon_base_info_id = Input::get('coupon_base_info_id');

            $key=base64_encode(json_encode(array("coupon_id"=>$coupon_id)));
            $temp=Cache::get($key);
            if($temp){
                return Lang::get("scs_error.commit_error_10012");
            }else{
                Cache::put($key,$key,0.1);
            }

            //判断推送列表是否有有效的数据
            $pushList = CouponPush::Coupon($users->id);
            if(!is_null($pushList)){
                $coupons = CouponBaseInfo::getCouponBaseInfo($pushList->coupon_base_info_id);
                if(!is_null($coupons)){
                   //判断优惠券是否有效
                    if($coupons->status == 1){
                        $coupon = Coupons::where('id',$pushList->coupon_id)->where('coupon_base_info_id',$pushList->coupon_base_info_id)->where('received_user_id',$users->id)->where('status',2)->first();
                        if(is_null($coupon)){
                            CouponPush::delPush($pushList);
                        }else{
                            if($coupons->date_type == 1){
                                $begin_date = date('Y-m-d',strtotime($coupon->get_time));
                                $end_date = $coupon->end_date;
                            }else{
                                $begin_date = $coupons->begin_date;
                                $end_date = $coupons->end_date;
                            }
                            //判断时间
                            if($begin_date <= date("Y-m-d",time()) && $end_date >= date("Y-m-d",time())){
                            }else{
                                CouponPush::delPush($pushList);
                            }
                        }
                    }else{
                        CouponPush::delPush($pushList);
                    }
                }else{
                    CouponPush::delPush($pushList);
                }
            }

            $pushList = CouponPush::Coupon($users->id);
            if(!empty($pushList)){
                $response = Lang::get("error.coupon_push_error_71009");
                break;
            }

            //获取优惠券领取时间
            $coupon = Coupons::where('id',$coupon_id)->where('coupon_base_info_id',$coupon_base_info_id)->where('received_user_id',$users->id)->where('status',2)->first();
            if(empty($coupon)){
                $response = Lang::get("error.coupon_not_exit_error_51004");
                break;
            }
            //判断优惠券是否使用
            if($coupon->used_user_id != 0){
                $response = Lang::get("error.coupon_push_used_error_71010");
                break;
            }

            //获取优惠券基本信息
            $coupon_base_info = CouponBaseInfo::getCouponBaseInfo($coupon_base_info_id);
            if(empty($coupon_base_info)){
                $response = Lang::get("error.coupon_push_used_error_71010");
                break;
            }

            if($coupon_base_info->status != 1){
                $response = Lang::get("error.coupon_status_error_51005");
                break;
            }

            //判断优惠券有效期
            if($coupon_base_info->date_type == 0){
                if($coupon_base_info->begin_date > CouponPush::date_time()){
                    $response = Lang::get("error.coupon_push_Invalid_error_71011");
                    break;
                }elseif($coupon_base_info->end_date < CouponPush::date_time()){
                    $response = Lang::get("error.coupon_game_over_error_51006");
                    break;
                }else{
                    try{
                        $coupon_push = new CouponPush();
                        $coupon_push->coupon_id = $coupon_id;
                        $coupon_push->coupon_base_info_id = $coupon_base_info_id;
                        $coupon_push->user_id = $users->id;
                        $coupon_push->phone = $users->phone;
                        $coupon_push->code = $coupon->code;
                        $coupon_push->status = 1;
                        $coupon_push->created_at = CouponPush::now_time();
                        $coupon_push->updated_at = CouponPush::now_time();
                        if(!$coupon_push->save()){
                            $response = Lang::get("error.coupon_push_not_error_71012");
                            break;
                        }
                    }catch(Exception $e){
                        Log::error("error", array("system" => "api3.0" ,"event" => "pushCoupon", 'input' =>Input::all() ,"exception" => $e));
                    }
                }
            }else{
                $res = CouponPush::Limit_time($coupon->get_time, $coupon->date_num);
                if($res == 1){
                    try{
                        $coupon_push = new CouponPush();
                        $coupon_push->coupon_id = $coupon_id;
                        $coupon_push->coupon_base_info_id = $coupon_base_info_id;
                        $coupon_push->user_id = $users->id;
                        $coupon_push->phone = $users->phone;
                        $coupon_push->code = $coupon->code;
                        $coupon_push->status = 1;
                        $coupon_push->created_at = CouponPush::now_time();
                        $coupon_push->updated_at = CouponPush::now_time();
                        if(!$coupon_push->save()){
                            $response = Lang::get("error.coupon_push_not_error_71012");
                            break;
                        }
                    }catch(Exception $e){
                        Log::error("error", array("system" => "api3.0" ,"event" => "pushCoupon", 'input' =>Input::all() ,"exception" => $e));
                    }
                }
            }

            $response = [
                'coupon_id' => $coupon_id,
            ];

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $response);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "pushCoupon",  'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /**
     * @Author:      sqq
     * @DateTime:    2018-07-28 18:14:16
     * @Description: 取消优惠券推送
     * @link:
     */
    public function cancelPushCoupon(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'coupon_id' => 'required',
                'coupon_base_info_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok');
        do{
            if($validator->fails()) {
                $response = Lang::get("error.args_error_10000");
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users=$this->verificationUserToken(Input::get("token"),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $coupon_id = Input::get('coupon_id');
            $coupon_base_info_id = Input::get('coupon_base_info_id');

            $key=base64_encode(json_encode(array("coupon_id"=>$coupon_id)));
            $temp=Cache::get($key);

            try{
                //判断推送列表是否有有效的数据
                $pushList = CouponPush::validCoupon($users->id, $coupon_id, $coupon_base_info_id);
                if(empty($pushList)){
                    $response = Lang::get("error.coupon_not_push_error_71015");
                    break;
                }
                $pushList->status = 2;
                $pushList->updated_at = CouponPush::now_time();
                if(!$pushList->save()){
                    $response = Lang::get("error.coupon_cancel_push_error_71014");
                    break;
                }
            }catch(Exception $e){
                Log::error("error", array("system" => "api3.0" ,"event" => "cancelPushCoupon", 'input' =>Input::all() ,"exception" => $e));
            }

            $response = [
                'coupon_id' => $coupon_id,
            ];

            $response = array('code' => 0, 'msg' => 'ok', 'result' => $response);
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "cancelPushCoupon",  'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


   /*
    * @Author:      sqq
    * @DateTime:    2018-08-02 08:55:32
    * @Description: 优惠券列表
    * @param  int     $status      [1-可用 2-已过期 3-未生效]
    * @link：http://www.activity.com/wechat/api3.0/coupon/myCoupon?token=668c155fab8dd6ef1e789b33ff6d040e&status=3
    * */
    public function myCoupon(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'status' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('error.args_error_10000');
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user = $this->verificationUserToken(Input::get('token'),$response);
            if(is_null($wechat_user)){
                break;
            }

            //获取参数
            if(Input::get('status') == 1 || Input::get('status') == 3){
                $coupons = Coupons::getCouponsList($wechat_user->id,2);
            }else{
                $coupons = Coupons::CouponsList($wechat_user->id);
            }

            $coupons_list = array();
            if(!is_null($coupons)){
                $coupons = $coupons->toArray();
                foreach($coupons as $k => $v){
                    $coupon_base_info = CouponBaseInfo::getCouponBaseInfo($v['coupon_base_info_id']);
                    if(!is_null($coupon_base_info)){
                        $_coupon = array();
                        if($coupon_base_info->date_type == 1){
                            $begin_date = date('Y-m-d',strtotime($v['get_time']));
                            $end_date = $v['end_date'];
                        }else{
                            $begin_date = $coupon_base_info->begin_date;
                            $end_date = $coupon_base_info->end_date;
                        }
                        if((int)Input::get('status') === 1 && (strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59')) > 0){
                            continue;
                        }
                        if((int)Input::get('status') === 1 && (strtotime(Common::currentTimeString()) - strtotime($begin_date.' 00:00:00')) < 0){
                            continue;
//                            unset($coupons[$k]);
//                            array_values($coupons);
                        }
                        if((int)Input::get('status') === 3 && strtotime(Common::currentTimeString()) - strtotime($begin_date.' 00:00:00') > 0){
                            continue;
                        }

                        if((int)Input::get('status') === 2 && strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') > 0 && $v['used_user_id'] != 0){
                            continue;
                        }

                        if((int)Input::get('status') === 2 && strtotime(Common::currentTimeString()) - strtotime($begin_date.' 00:00:00') > 0 && strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') < 0){
                            continue;
                        }

                        if((int)Input::get('status') === 2 && strtotime(Common::currentTimeString()) - strtotime($begin_date.' 00:00:00') < 0){
                            continue;
                        }

                        if($coupon_base_info->limit_type == 2){
                            $pack_id = CouponPackLimit::where('coupon_base_info_id',$coupon_base_info->id)->lists('pack_id');
                            $product_pack= ProductPackage::whereIn('id',$pack_id)->lists('name');
                        }else{
                            $product_pack = array();
                        }
                        //查询是否推送
                        $_coupon['is_push'] = CouponPush::where('coupon_base_info_id',$coupon_base_info->id)->where('coupon_id',$v['id'])->where('status',1)->where('user_id',$wechat_user->id)->count();  //0-未推送 1-已推送
                        $_coupon["id"] = $v['id'];
                        $_coupon['code'] = $v['code'];
                        $_coupon['amount'] = bcdiv($coupon_base_info->amount,100,2);
                        $_coupon['name'] = $coupon_base_info->name;
                        $_coupon['limit_amount'] =  bcdiv($coupon_base_info->limit_amount,100,2);
                        $_coupon['begin_date'] = $begin_date;
                        $_coupon['end_date'] = $end_date;
                        $_coupon['coupon_type'] = $coupon_base_info->type;
                        $_coupon['type'] = Coupons::$type[$coupon_base_info->type];
                        $_coupon['status'] = Coupons::$statsu[$v['status']];
                        $_coupon['limit_order'] = $coupon_base_info->limit_order;
                        $_coupon['limit_car'] = $coupon_base_info->limit_car;
                        $_coupon['used_car_no'] = $v['used_car_no'];
                        $_coupon["phone"] = $wechat_user->phone;
                        $_coupon["discount"] = $coupon_base_info->discount;
                        $_coupon["limit_book"] = $coupon_base_info->limit_book;
                        $_coupon["product_pack"] = $product_pack;
                        $_coupon["limit_type"] = $coupon_base_info->limit_type;
                        $_coupon["coupon_base_info_id"] = $coupon_base_info->id;
                        array_push($coupons_list, $_coupon);
                    }
                }
            }
            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => [
                    'count' => count($coupons_list),
                    'coupon' => $coupons_list,
                ],
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "myCoupon", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    /*
   * @Author:      sqq
   * @DateTime:    2018-08-09 13:05:32
   * @Description: 优惠券详情
   * @param $id   int     优惠券id
   * @link：http://www.activity.com/wechat/api3.0/coupon/couponShow?token=668c155fab8dd6ef1e789b33ff6d040e&id=494302
   * */
    public function couponShow(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('error.args_error_10000');
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users = $this->verificationUserToken(Input::get('token'),$response);
            if(is_null($users)){
                break;
            }

            //获取参数
            $id = Input::get('id');   //优惠券id
            $coupon = Coupons::where('id',$id)->where('received_user_id',$users->id)->first();
            if(empty($coupon)){
                $response = Lang::get("error.coupon_not_exit_error_51004");
                break;
            }

            //判断优惠券是否有效
            $coupon_base_info = CouponBaseInfo::getCouponBaseInfo($coupon->coupon_base_info_id);
            if(!is_null($coupon_base_info)){
                $_coupon = array();

                if($coupon_base_info->date_type == 1){
                    $begin_date = date('Y-m-d',strtotime($coupon->get_time));
                    $end_date = $coupon->end_date;
                }else{
                    $begin_date = $coupon_base_info->begin_date;
                    $end_date = $coupon_base_info->end_date;
                }
                $coupon->begin_date = $begin_date;
                $coupon->end_date = $end_date;
                $coupon->phone = $users->phone;
                if(strtotime(Common::currentTimeString()) - strtotime($begin_date.' 00:00:00') > 0 && strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') < 0){
                    $coupon->type = 2;   //有效
                }

                if(strtotime(Common::currentTimeString()) - strtotime($end_date.' 23:59:59') > 0){
                    $coupon->type = 3;   //已过期
                }

                if(strtotime(Common::currentTimeString()) - strtotime($begin_date.' 00:00:00') < 0){
                    $coupon->type = 1;   //未生效
                }

                if($coupon_base_info->limit_type == 2){
                    $pack_id = CouponPackLimit::where('coupon_base_info_id',$coupon_base_info->id)->lists('pack_id');
                    $product_pack= ProductPackage::whereIn('id',$pack_id)->lists('name');
                }else{
                    $product_pack = array();
                }

                if(!empty($product_pack)){
                    $product_pack = implode('、',$product_pack);
                }
            }else{
                $response = Lang::get("error.coupon_not_exit_error_51004");
                break;
            }

            //查询适配的城市
            $coupon_shop = '';
            $coupon_shop['city'] = CouponCity::join('resource_city_type','coupon_city.city_id','=','resource_city_type.id')
                ->where('resource_city_type.status',1)
                ->where('coupon_city.status',1)
                ->where('coupon_city.coupon_base_id',$coupon_base_info->id)
                ->select('resource_city_type.id','resource_city_type.name')
                ->get();

            //查询适配门店
            if(!is_array($coupon_shop['city'])){
                foreach($coupon_shop['city'] as $key => $val){
                    $coupon_shop['city'][$key]['shops'] = CouponShop::join('maintenance_shops','coupon_shop.shop_id','=','maintenance_shops.id')
                        ->where('coupon_shop.coupon_base_id',$coupon_base_info->id)
                        ->where('maintenance_shops.city_id',$val->id)
                        ->where('coupon_shop.status',1)
                        ->select('maintenance_shops.id','maintenance_shops.name')
                        ->get();
                }
            }


            //查询是否推送
            $coupon->is_push = CouponPush::where('coupon_base_info_id',$coupon_base_info->id)->where('coupon_id',$id)->where('status',1)->where('user_id',$users->id)->count();  //0-未推送 1-已推送

            //匹配状态值
            $coupon->status_chinese = Coupons::$statsu[$coupon->status];
            $coupon_base_info->type_chinese = Coupons::$type[$coupon_base_info->type];

            $response = array(
                'code' => 0,
                'msg' => 'ok',
                'result' => [
                    'coupon' => $coupon,
                    'coupon_base_info' => $coupon_base_info,
                    'service' => $product_pack,
                    'coupon_shop' => $coupon_shop,
                ],
            );
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "myCoupon", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }


    public function AipOcr(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'file' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('error.args_error_10000');
                $response["result"]=$validator->messages()->all();
                break;
            }
            $users = $this->verificationUserToken(Input::get('token'),$response);
            if(is_null($users)){
                break;
            }
            //获取参数
            $appId = Config::get('config.baidu_AI_appId_url');
            $apiKey = Config::get('config.baidu_AI_apiKey_url');
            $secretKey = Config::get('config.baidu_AI_secretKey_url');
            if(!$appId || !$apiKey || !$secretKey){
                $response = Lang::get('error.baidu_AI_not_exist_10000');
                break;
            }
            $client = new AipOcr($appId,$apiKey,$secretKey);
            header("Access-Control-Allow-Origin: *");
            header("Access-Control-Allow-Methods:POST,GET");
            header("Access-Control-Allow-Headers:x-requested-with,content-type");
            header("Content-type:text/json;charset=utf-8");
            $image = Input::file('file');
            //Log::info("info", array("system" => "wechat2.0" ,"event" => "myCoupon", 'input' =>Input::all() ,"output" => $image));
            //$image = $this->base64EncodeImage($image);
            $image = file_get_contents($image);
            $res = $client->basicGeneral($image);  //普通版
            //$res = $client->basicAccurate($image); //高精度版
            //$res = $client->vehicleLicense($image); //行驶证识别
           // $res = $client->webImage($image); //网络图片文字识别

            //$templateSign = "Nsdax2424asaAS791823112";
            //$res = $client->custom($image, $templateSign);  //自定义模板文字识别
            if(!array_key_exists('error_code',$res)){
                //处理数据
                $words = Config::get('config.baidu_AI_Intercept_begin');
                $result_word = '';
                if($res['words_result']){
                    $baidu_AI_Intercept_begin = Config::get('config.baidu_AI_Intercept_begin');
                    $baidu_AI_strsub_begin = Config::get('config.baidu_AI_strsub_begin');
                    foreach($res['words_result'] as $_key => $_val){
                        $arr = mb_substr($_val['words'],0,1,'utf-8');
                        if($arr == $baidu_AI_Intercept_begin){
                            $result_word = explode($baidu_AI_strsub_begin,$_val['words']);
                        }
                    }
                }
                if(is_array($result_word) && array_key_exists('1',$result_word)){
                    $result_word = $result_word[1];
                    $response = array(
                        'code' => 0,
                        'msg' => 'ok',
                        'result' => [
                            'words' => $result_word
                        ],
                    );
                }else{
                    $response = Lang::get('error.baidu_AI_AipOcr_error_10001');
                    break;
                }
            }else{
                $response = Lang::get('error.baidu_AI_AipOcr_error_10001');
                break;
            }
        }while(false);
        Log::info("info", array("system" => "wechat2.0" ,"event" => "myCoupon", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }



    public function base64EncodeImage ($image_file) {
        $base64_image = '';
        $image_info = getimagesize($image_file);
        $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
        $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
        return $base64_image;
    }

    /*
      * @Author:      sqq
      * @DateTime:    2018-11-17 10:21:10
      * @Description: 优惠券兑换 (先判断是否有适配的口令，再判断是否有适配的券码)
     * @Url: http://www.activity.com/wechat/api3.0/coupon/exchangeCoupon?token=09a49161d729a39f915b7fccde455bfd&code=%E6%81%AD%E5%96%9C%E5%8F%91%E8%B4%A2
      * */
    public function exchangeCoupon(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'code' => 'required',
            )
        );
        do{
            if($validator->fails()){
                $response = Lang::get('error.args_error_10000');
                $response["result"]=$validator->messages()->all();
                break;
            }
            $wechat_user = $this->verificationUserToken(Input::get('token'),$response);
            if(is_null($wechat_user)){
                break;
            }
            $password = CouponBaseInfo::where('password',trim(Input::get('code')))->where('status',1)->where('num_type',0)->get();
            if(!is_null($password)){
                $time = date("Y-m-d H:i:s",time());
                $array = array();
                $password_ids = array_column(json_decode(json_encode($password),true),'id');
                foreach($password as $key => $val){
                    if($val->date_type == 0 && $val->begin_date.' 00:00:00' > $time){
                        break;
                    }
                    if($val->date_type == 0 &&$val->end_date.' 23:59:59' < $time){
                        break;
                    }
                    if($val->limit_num != 0){
                        $coupons_num = Coupons::where('coupon_base_info_id',$val->id)->where('received_user_id',$wechat_user->id)->count();
                        if($coupons_num >= $val->limit_num){
                            array_push($array,$val->id);
                            break;
                        }
                    }
                    $coupons_two = Coupons::where('coupon_base_info_id',$val->id)->where('received_user_id',0)->first();
                    if(!empty($coupons_two)){
                        try{
                            $coupons_id = $coupons_two->id;
                            $coupon_edit = Coupons::find($coupons_id);
                            $coupon_edit->received_user_id = $wechat_user->id;
                            $coupon_edit->status = 2;
                            if($val->date_type == 1){
                                $coupon_edit->end_date = date("Y-m-d", strtotime("+" . $val->date_num . " day"));
                            }
                            $coupon_edit->get_time = Common::currentTimeString();
                            $coupon_edit->save();
                        }catch (Exception $e){
                            Log::error("error", array("system" => "api3.0" ,"event" => "exchangeCoupon", 'input' =>Input::all() ,"exception" => $e));
                            $response = Lang::get("error.coupon_exchange_error_70003");
                            break;
                        }
                    }

                }
                $diff_array = array_diff($array,$password_ids);
                if(empty($diff_array)){
                    $response = Lang::get('error.coupon_received_error_51007');
                    break;
                }
            }else{
                $coupons = Coupons::where('code',trim(Input::get('code')))->first();
                if(empty($coupons)){
                    $response = Lang::get('error.redeem_code_invalid_error_71006');
                    break;
                }
                $coupon_base_info = CouponBaseInfo::where('id',$coupons->coupon_base_info_id)->first();
                if(empty($coupon_base_info)){
                    $response = Lang::get('error.redeem_code_invalid_error_71006');
                    break;
                }
                if($coupon_base_info->code_type == 1){
                    if($coupons->received_user_id){
                        $response = Lang::get('error.redeem_code_user_error_71008');
                        break;
                    }
                }
                if($coupon_base_info->limit_num != 0){
                    $coupons_num = Coupons::where('coupon_base_info_id',$coupon_base_info->id)->where('received_user_id',$wechat_user->id)->count();
                    if($coupons_num >= $coupon_base_info->limit_num){
                        $response = Lang::get('error.redeem_code_invalid_or_game_over_71007');
                        break;
                    }
                }
                if($coupon_base_info->code_type == 2){
                    $coupons_two = Coupons::where('code',Input::get('code'))->where('received_user_id',0)->first();
                    if(empty($coupons_two)){
                        $response = Lang::get('error.redeem_code_invalid_error_71006');
                        break;
                    }
                    $coupons_two_id = $coupons_two->id;
                }
                if($coupon_base_info->status == 0){
                    $response = Lang::get('error.redeem_code_invalid_error_71006');
                    break;
                }
                $time = date("Y-m-d H:i:s",time());
                if($coupon_base_info->date_type == 0 && $coupon_base_info->begin_date.' 00:00:00' > $time){
                    $response = Lang::get('error.coupon_exchange_time_error_70001');
                    break;
                }
                if($coupon_base_info->date_type == 0 &&$coupon_base_info->end_date.' 23:59:59' < $time){
                    $response = Lang::get('error.redeem_code_game_over_71005');
                    break;
                }
                try{
                    $coupons_id = isset($coupons_two_id)?$coupons_two_id:$coupons->id;
                    $coupon_edit = Coupons::find($coupons_id);
                    $coupon_edit->received_user_id = $wechat_user->id;
                    $coupon_edit->status = 2;
                    if($coupon_base_info->date_type == 1){
                        $coupon_edit->end_date = date("Y-m-d", strtotime("+" . $coupon_base_info->date_num . " day"));
                    }
                    $coupon_edit->get_time = Common::currentTimeString();
                    $coupon_edit->save();
                }catch (Exception $e){
                    Log::error("error", array("system" => "wechat2.0" ,"event" => "exchangeCoupon", 'input' =>Input::all() ,"exception" => $e));
                    $response = Lang::get("error.coupon_exchange_error_70003");
                    break;
                }
            }

            $response = array(
                "code" => 0,
                "msg" => "ok",
            );
            //ActivityCouponInfo::where('coupon_base_info_id',$coupons->coupon_base_info_id)->groupBy('activity_id')->get();
        }while(false);
        Log::info("info", array("system" => "api3.0" ,"event" => "exchangeCoupon", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

}